<?php
namespace Home\Controller;
use Think\Controller;
class CourseController extends Controller {
   public function getallcourse(){
        $courseModel=M('course');
        $course=$courseModel->order("cid")->select();
        // echo json_encode($data);  
        // dump($course);
        $this->course=$course;
        $this->display();
    }
    public function getcourseById(){
        $cid=$_GET['cid'];
        $courseModel=M('course');
        $course=$courseModel->where("courseid=$cid")->find();
        $this->course=$course;
        $this->display();
        // var_dump($course);
    }
   public function delcourse(){
    $cid=$_GET['cid'];
    $orderModel=M('course');
    if($orderModel->delete($cid)){
        $this->success('删除成功');
    }else{
        $this->error('删除失败');
    }
   }
   //显示修改界面

   public function updatecourse($cid){
    $courseModel=M('course');
    $course=$courseModel->where("cid=$cid")->find();
    $this->course=$course;
    $this->display();
    // var_dump($course);
    // echo json_encode($one);
   }
    public function updatecourseInfo(){

            $data=$_POST;
            $file=$_FILES;
            $cid=$data['cid'];
            // dump($data);
            // dump($cid);
                    
            $courseModel = M('course'); // 实例化User模型
            // 要修改的数据对象属性赋值
            // $postModel->create();   // 根据表单提交的POST数据创建数据对象
            //dump($data);
        // $result=$postModel->where("postid=$postid")->save($data);
            $createresult=$courseModel->create();
        if($file['cphoto']['size']==0) {
            //没有修改头像
            $result=$courseModel->where("cid=$cid")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 777888800 ;// 设置附件上传大小
            $upload->exts  = array('jpg', 'gif', 'png', 'jpeg','mp4','wmv');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/postcourse/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(10,99);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                $data['cphoto']= $info['cphoto']['savename']; 

                $result=$courseModel->where("cid=$cid")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/postcourse/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
        if($result){
             $this->success('修改成功！','getallcourse');
             }
            else{
                $this->error('修改失败！');
                }
    }  
}